cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(recipe-03 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(sum_integers sum_integers.cpp)

option(ENABLE_UNIT_TESTS "enable unit tests" ON)                            # 定义一个变量，设置成true
message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}")

if (ENABLE_UNIT_TESTS)
    include(FetchContent)                                                   # 下载的头文件

    # 下载带有特定标记的Git外部库
    FetchContent_Declare(
            googletest
            GIT_REPOSITORY https://github.com/google/googletest.git
            GIT_TAG        release-1.11.0
    )
    FetchContent_GetProperties(googletest)                                  # 获得属性
    FetchContent_Populate(googletest)                                       # 下载、定义googletest_SOURCE_DIR 和 googletest_BINARY_DIR

    add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})     # 把googletest的头文件和二进制文件包含在项目中
    message(STATUS "googletest_SOURCE_DIR: ${googletest_SOURCE_DIR}")
    message(STATUS "googletest_BINARY_DIR: ${googletest_BINARY_DIR}")

    add_executable(cpp_test test.cpp)
    target_link_libraries(cpp_test sum_integers gtest_main)

    enable_testing()                                                        # 添加测试
    add_test(
            NAME google_test
            COMMAND $<TARGET_FILE:cpp_test>
    )
endif ()
